﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using CUWo.App.Resources;
using CUWo.Common;
using CUWo.Common.Entities;
using CUWo.Common.StorageHelper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace CUWo.App.Views
{
    public partial class UserCenterPage : PhoneApplicationPage
    {
        public UserCenterPage()
        {
            InitializeComponent();

            this.AddHeaderToPivotItemInPivotMain();
        }

        void LoadRomInfo()
        {
            this.ucUserCenterRegister.txtMobile.Text = AppResources.UserCenterRegisterInputChinaUnicomMobile;
            this.ucUserCenterLogin.txtMobile.Text = AppResources.UserCenterRegisterInputChinaUnicomMobile;
            this.ucUserCenterLogin.txtConfirmMobile.Text = AppResources.UserCenterRegisterInputChinaUnicomMobile;

            if (!IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                return;
            }
            var romInfoString = IsolatedStorageHelper.Instance.ReadText(Keys.ROM_FILENAME);
            if (!string.IsNullOrWhiteSpace(romInfoString))
            {
                RomInfo romInfo = JSONHelper.Deserialize<RomInfo>(romInfoString);
                if (romInfo != null)
                {
                    if (!string.IsNullOrWhiteSpace(romInfo.Mobile))
                    {
                        this.ucUserCenterRegister.txtMobile.Text = romInfo.Mobile;
                        this.ucUserCenterLogin.txtMobile.Text = romInfo.Mobile;
                        this.ucUserCenterLogin.txtConfirmMobile.Text = romInfo.Mobile;
                        //TextBlock content = this.ucUserCenterSetting.FindVisualChild(this.ucUserCenterSetting.tgsPush, "txtToggleSwitchStatus");
                        //if (content != null)
                        //{
                        //    content.Text = romInfo.IsPush ? "开启" : "关闭";
                        //}
                    }
                }
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            //
            //TODO 保存”消息"中的已读标识数据
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            if (this.ucUserCenterLogin.gridForgetPassword.Visibility == System.Windows.Visibility.Visible)
            {
                this.ucUserCenterLogin.gridForgetPassword.Visibility = System.Windows.Visibility.Collapsed;
                this.ucUserCenterLogin.gridLogin.Visibility = System.Windows.Visibility.Visible;
                e.Cancel = true;
            }
            if (this.ucUserCenterSetting.gridSettingMain.Visibility == System.Windows.Visibility.Collapsed)
            {
                this.ucUserCenterSetting.gridSettingMain.Visibility = System.Windows.Visibility.Visible;
                this.ucUserCenterSetting.gridSettingAbout.Visibility = System.Windows.Visibility.Collapsed;
                this.ucUserCenterSetting.gridSettingQA.Visibility = System.Windows.Visibility.Collapsed;
                this.ucUserCenterSetting.gridSettingSuggest.Visibility = System.Windows.Visibility.Collapsed;
                e.Cancel = true;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //
            if (this.NavigationContext.QueryString != null && this.NavigationContext.QueryString.Count > 0)
            {
                string pivotitem = this.NavigationContext.QueryString["pivotitem"];
                if (!string.IsNullOrWhiteSpace(pivotitem))
                {

                    switch (pivotitem)
                    {
                        case "邮箱": this.pivot.SelectedItem = this.pivotEmail; break;
                        case "消息": this.pivot.SelectedItem = this.pivotMessage; break;
                        case "登录": this.pivot.SelectedItem = this.pivotLogin; break;
                        case "密码修改": this.pivot.SelectedItem = this.pivotUpdatePassword; break;
                        default: this.pivot.SelectedItem = this.pivotSetting; break;
                    }
                }
            }
        }

        private void AddHeaderToPivotItemInPivotMain()
        {
            double fontSize = 36;
            Color foreground = CUWo.Common.Helper.HexToColor("#ff000000");
            Thickness margin = new Thickness(-10, -10, 0, 10);
            this.pivotSetting.Header = new PivotItemHelper().CreateTextBlock("设置", fontSize, foreground, margin);
            this.pivotMessage.Header = new PivotItemHelper().CreateTextBlock("消息", fontSize, foreground, margin);
            this.pivotEmail.Header = new PivotItemHelper().CreateTextBlock("邮箱", fontSize, foreground, margin);
            this.pivotLogin.Header = new PivotItemHelper().CreateTextBlock("登录", fontSize, foreground, margin);
            this.pivotRegister.Header = new PivotItemHelper().CreateTextBlock("注册", fontSize, foreground, margin);
            this.pivotUpdatePassword.Header = new PivotItemHelper().CreateTextBlock("密码修改", fontSize, foreground, margin);
        }

        private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PivotItem item = this.pivot.SelectedItem as PivotItem;
            if (item == null)
            {
                return;
            }

            LoadRomInfo();
            if (this.pivotRegister == item)
            {
                ApplicationBar = ((ApplicationBar)this.Resources["appBarRegister"]);
            }
            else if (this.pivotLogin == item)
            {
                ApplicationBar = ((ApplicationBar)this.Resources["appBarLogin"]);
            }
            else if (this.pivotUpdatePassword == item)
            {
                ApplicationBar = ((ApplicationBar)this.Resources["appBarUpdatePassword"]);
            }
            else if (this.pivotMessage == item)
            {
                this.ucUserCenterMessage.BindData();
            }
            else if (this.pivotEmail == item)
            {
                this.ucUserCenterEmail.BindData();
            }
            else
            {
                ApplicationBar = new ApplicationBar();
                ApplicationBar.IsVisible = false;
            }

        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void appBarRegister_Click(object sender, EventArgs e)
        {
            this.Focus();//隐藏软键盘
            this.ucUserCenterRegister.MobileRegister();
        }


        /// <summary>
        /// 登录/忘记密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void appBarConfirm_Click(object sender, EventArgs e)
        {
            this.Focus();//隐藏软键盘
            this.ucUserCenterLogin.MobileLoginOrForgetPassword();
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void appBarUpdatePassword_Click(object sender, EventArgs e)
        {
            this.Focus();//隐藏软键盘
            this.ucUserCenterUpdatePassword.UpdatePassword();
        }


    }
}